<template>
  <el-dialog
    :title="titleName"
    :visible="addShowDialog"
    width="30%"
    @close="btnCancel"
  >
    <el-form
      label-width="120px"
      :rules="rules"
      :model="formData"
      ref="formData"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="formData.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="formData.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="formData.manager"
          @focus="getEmployeesSimple"
          placeholder="请选择部门负责人"
        >
          <el-option
            v-for="user in people"
            :key="user.id"
            :label="user.username"
            :value="user.username"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          type="textarea"
          :row="3"
          placeholder="1-300个字符"
          style="width: 80%"
        />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="btnCancel">取消</el-button>

        <el-button type="primary" size="small" @click="submitForm"
          >确定</el-button
        >
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import {
  getDepartmentsList,
  addDepartment,
  getDepartment,
  editDepartment,
} from "@/api/departments";
import { getEmployeesSimple } from "@/api/employees";
export default {
  name: "addDepartDialog",
  props: {
    addShowDialog: {
      type: Boolean,
      default: false,
    },
    treeNode: {
      type: Object,
      default: null,
    },
  },
  data() {
    //检测部门是否重复
    const checkNameRepeat = async (rules, value, callback) => {
      //获取组织架构信息
      const { depts } = await getDepartmentsList();

      let repeat = false;
      if (this.formData.id) {
        //编辑部门下
        //判读是否在同级部门下有相同的名字(不包括自己)
        repeat = depts
          .filter(
            (item) =>
              item.pid === this.treeNode.pid && item.id !== this.treeNode.id
          )
          .some((item) => item.name === value);
      } else {
        //新增部门下
        //filter筛选出组织架构内的子节点 some判断是否有相同的名字
        repeat = depts
          .filter((item) => item.pid === this.treeNode.id)
          .some((item) => item.name === value);
      }
      repeat ? callback(new Error("该部门下已经有" + value)) : callback();
    };
    const checkCodeRepeat = async (rules, value, callback) => {
      //获取组织架构信息
      const { depts } = await getDepartmentsList();
      let repeat = false;
      if (this.formData.id) {
        //编辑部门下
        //除了自己本身的code可以相同,其余不能相同
        repeat = depts
          .filter((item) => item.id !== this.treeNode.id)
          .some((item) => item.code === value && value);
      } else {
        //新增部门下
        // some判断是否有相同的Code
        //不能有相同的code
        repeat = depts.some((item) => item.code === value && value);
      }

      repeat ? callback(new Error(`已经有这个部门编码${value}`)) : callback();
    };
    return {
      // 表单信息
      formData: {
        name: "",
        code: "",
        manager: "",
        introduce: "",
      },
      //校验规则
      rules: {
        name: [
          { required: true, message: "部门名称不能为空", tirgger: blur },
          { min: 1, max: 50, message: "部门名称长度1~50字", tirgger: blur },
          { tirgger: blur, validator: checkNameRepeat },
        ],
        code: [
          { required: true, message: "编码名称不能为空", tirgger: blur },
          { min: 1, max: 50, message: "部门编码长度1~50字", tirgger: blur },
          { tirgger: blur, validator: checkCodeRepeat },
        ],
        manager: [
          { required: true, message: "部门负责人不能为空", tirgger: blur },
        ],
        introduce: [
          { required: true, message: "部门介绍不能为空", tirgger: blur },
          { min: 1, max: 300, message: "部门介绍长度1~300字", tirgger: blur },
        ],
      },
      people: [],
    };
  },
  computed: {
    titleName() {
      return this.formData.id ? "编辑部门" : "新增部门";
    },
  },
  methods: {
    async getEmployeesSimple() {
      this.people = await getEmployeesSimple();
    },
    //获取详情方法
    async getDepartment(id) {
      this.formData = await getDepartment(id);
      this.$nextTick(() => this.$refs.formData.clearValidate());
    },
    //提交表单
    submitForm() {
      this.$refs.formData.validate(async (isok) => {
        if (isok) {
          //判读是formData否有id决定是编辑还是添加
          if (this.formData.id) {
            await editDepartment(this.formData);
          } else {
            const data = { ...this.formData, pid: this.treeNode.id };
            await addDepartment(data);
          }

          //提交完成重新渲染页面
          this.$emit("addDepts");
          //关闭Dialog sync方法
          this.$emit("update:addShowDialog", false);
        }
      });
    },
    // 点击取消关闭Dialog ,并重置校验
    btnCancel() {
      (this.formData = {
        name: "",
        code: "",
        manager: "",
        introduce: "",
      }),
        this.$refs.formData.resetFields();
      this.$emit("update:addShowDialog", false);
    },
  },
};
</script>

<style>
</style>